<?php

require_once 'Surat.php';

class Surat_masuk extends Surat{

	private $asal;
	private $waktu_terima;
	private $penerima;
	private $no_agenda;
	
	function setAsal($asal){
		$this->asal = $asal;
	}
	
	function setWaktu_terima($waktu_terima){
		$this->waktu_terima = $waktu_terima;
	}
	
	function setPenerima($penerima){
		$this->penerima = $penerima;
	}
	
	function getAsal(){
		return $this->asal;
	}
	
	function getWaktu_terima(){
		return $this->waktu_terima;
	}
	
	function getPenerima(){
		return $this->penerima;
	}
	

}

class Daftar_surat_masuk{

	
	function getAll(){
		$sql="
		select *
		from
		surat_masuk
		order by kd_sm desc
		";
		
		$list_surat_masuk = array();
		
		$data = mysql_query($sql);
		if($data){
			while($row = mysql_fetch_assoc($data)){
			
				$surat_masuk = new Surat_masuk();
				$surat_masuk->setId($row['kd_sm']);
				$surat_masuk->setNomor($row['no_sm']);
				$surat_masuk->setTgl($row['tgl_sm']);
				$surat_masuk->setSifat($row['kd_sifat_surat']);
				$surat_masuk->setHal($row['hal_sm']);
				$surat_masuk->setLampiran($row['lampiran_sm']);
				$surat_masuk->setAsaL($row['asal_sm']);
				$surat_masuk->setWaktu_terima($row['waktu_terima_sm']);
				$surat_masuk->setPenerima($row['penerima_sm']);
				$surat_masuk->setNo_agenda($row['no_agenda_sm']);
				
				$list_surat_masuk[] = $surat_masuk;
			}
		}	
		return $list_surat_masuk;
	}
	
	function SearchByHal($hal){
		
		$hasil=array();
		
		foreach ($hal as $hals){
		
			$sql="select *
			from
			surat_masuk
			WHERE
			hal_sm LIKE '%".$hals."%'
			order by kd_sm desc
			
			";
		
			$list_surat_masuk = array();
		
			$data = mysql_query($sql);
				if($data){
					while($row = mysql_fetch_assoc($data)){
					
						$surat_masuk = new Surat_masuk();
						$surat_masuk->setId($row['kd_sm']);
						$surat_masuk->setNomor($row['no_sm']);
						$surat_masuk->setTgl($row['tgl_sm']);
						$surat_masuk->setSifat($row['kd_sifat_surat']);
						$surat_masuk->setHal($row['hal_sm']);
						$surat_masuk->setLampiran($row['lampiran_sm']);
						$surat_masuk->setAsaL($row['asal_sm']);
						$surat_masuk->setWaktu_terima($row['waktu_terima_sm']);
						$surat_masuk->setPenerima($row['penerima_sm']);
						$surat_masuk->setNo_agenda($row['no_agenda_sm']);
						
						$list_surat_masuk[] = $surat_masuk;
					}
				}	
			$hasil = array_merge($hasil,$list_surat_masuk);
			}
			return $hasil;
		}
		
		function SearchByHalTgl($hal, $tgl){
		
		$hasil=array();
		
		foreach ($hal as $hals){
		
			$sql="select *
			from
			surat_masuk
			WHERE tgl_sm = '$tgl' AND
			hal_sm LIKE '%".$hals."%'
			order by kd_sm desc
			
			";
		
			$list_surat_masuk = array();
		
			$data = mysql_query($sql);
				if($data){
					while($row = mysql_fetch_assoc($data)){
					
						$surat_masuk = new Surat_masuk();
						$surat_masuk->setId($row['kd_sm']);
						$surat_masuk->setNomor($row['no_sm']);
						$surat_masuk->setTgl($row['tgl_sm']);
						$surat_masuk->setSifat($row['kd_sifat_surat']);
						$surat_masuk->setHal($row['hal_sm']);
						$surat_masuk->setLampiran($row['lampiran_sm']);
						$surat_masuk->setAsaL($row['asal_sm']);
						$surat_masuk->setWaktu_terima($row['waktu_terima_sm']);
						$surat_masuk->setPenerima($row['penerima_sm']);
						$surat_masuk->setNo_agenda($row['no_agenda_sm']);
						
						$list_surat_masuk[] = $surat_masuk;
					}
				}	
			$hasil = array_merge($hasil,$list_surat_masuk);
			}
			return $hasil;
		}
		
		function searchByTgl($tgl){
		$sql="
		select *
		from
		surat_masuk
		where tgl_sm = '$tgl'
		order by kd_sm desc
		";
		
		$list_surat_masuk = array();
		
		$data = mysql_query($sql);
		if($data){
			while($row = mysql_fetch_assoc($data)){
			
				$surat_masuk = new Surat_masuk();
				$surat_masuk->setId($row['kd_sm']);
				$surat_masuk->setNomor($row['no_sm']);
				$surat_masuk->setTgl($row['tgl_sm']);
				$surat_masuk->setSifat($row['kd_sifat_surat']);
				$surat_masuk->setHal($row['hal_sm']);
				$surat_masuk->setLampiran($row['lampiran_sm']);
				$surat_masuk->setAsaL($row['asal_sm']);
				$surat_masuk->setWaktu_terima($row['waktu_terima_sm']);
				$surat_masuk->setPenerima($row['penerima_sm']);
				$surat_masuk->setNo_agenda($row['no_agenda_sm']);
				
				$list_surat_masuk[] = $surat_masuk;
			}
		}	
		return $list_surat_masuk;
	}
		
	
	function SearchByHalUnit($hal,$unit){
		
		$hasil=array();
		
		foreach ($hal as $hals){
		
			$sql="
			select a.*
			from
			surat_masuk a, disposisi b
			WHERE 
			a.kd_sm = b.kd_sm AND
			b.tujuan_disposisi = '$unit' and
			a.hal_sm LIKE '%".$hals."%'
			order by kd_sm desc
			
			";
		
			$list_surat_masuk = array();
		
			$data = mysql_query($sql);
				if($data){
					while($row = mysql_fetch_assoc($data)){
					
						$surat_masuk = new Surat_masuk();
						$surat_masuk->setId($row['kd_sm']);
						$surat_masuk->setNomor($row['no_sm']);
						$surat_masuk->setTgl($row['tgl_sm']);
						$surat_masuk->setSifat($row['kd_sifat_surat']);
						$surat_masuk->setHal($row['hal_sm']);
						$surat_masuk->setLampiran($row['lampiran_sm']);
						$surat_masuk->setAsaL($row['asal_sm']);
						$surat_masuk->setWaktu_terima($row['waktu_terima_sm']);
						$surat_masuk->setPenerima($row['penerima_sm']);
						$surat_masuk->setNo_agenda($row['no_agenda_sm']);
						
						$list_surat_masuk[] = $surat_masuk;
					}
				}	
			$hasil = array_merge($hasil,$list_surat_masuk);
			}
			return $hasil;
		}
	
	function SearchByHalTglUnit($hal,$tgl,$unit){
		
		$hasil=array();
		
		foreach ($hal as $hals){
		
			$sql="select a.*
			from
			surat_masuk a, disposisi b
			WHERE 
			a.kd_sm = b.kd_sm AND
			b.tujuan_disposisi = '$unit' AND
			a.tgl_sm = '$tgl' AND
			a.hal_sm LIKE '%".$hals."%'
			order by kd_sm desc
			
			";
		
			$list_surat_masuk = array();
		
			$data = mysql_query($sql);
				if($data){
					while($row = mysql_fetch_assoc($data)){
					
						$surat_masuk = new Surat_masuk();
						$surat_masuk->setId($row['kd_sm']);
						$surat_masuk->setNomor($row['no_sm']);
						$surat_masuk->setTgl($row['tgl_sm']);
						$surat_masuk->setSifat($row['kd_sifat_surat']);
						$surat_masuk->setHal($row['hal_sm']);
						$surat_masuk->setLampiran($row['lampiran_sm']);
						$surat_masuk->setAsaL($row['asal_sm']);
						$surat_masuk->setWaktu_terima($row['waktu_terima_sm']);
						$surat_masuk->setPenerima($row['penerima_sm']);
						$surat_masuk->setNo_agenda($row['no_agenda_sm']);
						
						$list_surat_masuk[] = $surat_masuk;
					}
				}	
			$hasil = array_merge($hasil,$list_surat_masuk);
			}
			return $hasil;
		}
		
	function SearchByTglUnit($tgl,$unit){
		
		$sql="
		select a.*
		from
		surat_masuk a, disposisi b
		where a.kd_sm = b.kd_sm AND a.tgl_sm = '$tgl' AND
		b.tujuan_disposisi = '$unit'
		order by a.kd_sm desc
		";
		
		$list_surat_masuk = array();
		
		$data = mysql_query($sql);
		if($data){
			while($row = mysql_fetch_assoc($data)){
			
				$surat_masuk = new Surat_masuk();
				$surat_masuk->setId($row['kd_sm']);
				$surat_masuk->setNomor($row['no_sm']);
				$surat_masuk->setTgl($row['tgl_sm']);
				$surat_masuk->setSifat($row['kd_sifat_surat']);
				$surat_masuk->setHal($row['hal_sm']);
				$surat_masuk->setLampiran($row['lampiran_sm']);
				$surat_masuk->setAsaL($row['asal_sm']);
				$surat_masuk->setWaktu_terima($row['waktu_terima_sm']);
				$surat_masuk->setPenerima($row['penerima_sm']);
				$surat_masuk->setNo_agenda($row['no_agenda_sm']);
				
				$list_surat_masuk[] = $surat_masuk;
			}
		}	
		return $list_surat_masuk;
	}
		
	
	function getByUnitDisposisi($kd_unit){
	
		$sql="
			select distinct(kd_sm) as kd_sm from disposisi where tujuan_disposisi = '$kd_unit'
		";
		
		$list_sm = array();
		
		$data = mysql_query($sql);
		if($data){
			while($row = mysql_fetch_assoc($data)){
			
					
				$list_sm[] = $row['kd_sm'];
			}
		}	
		
		$list_surat_masuk = array();
		
		foreach($list_sm as $sm){
			$sql="
			select *
			from
			surat_masuk
			where 
			kd_sm = '".$sm."'
			";
			$surat_masuk=false;
			$data = mysql_query($sql);
			if($data){
			while($row = mysql_fetch_assoc($data)){
			
				$surat_masuk = new Surat_masuk();
				$surat_masuk->setId($row['kd_sm']);
				$surat_masuk->setNomor($row['no_sm']);
				$surat_masuk->setTgl($row['tgl_sm']);
				$surat_masuk->setSifat($row['kd_sifat_surat']);
				$surat_masuk->setHal($row['hal_sm']);
				$surat_masuk->setLampiran($row['lampiran_sm']);
				$surat_masuk->setAsaL($row['asal_sm']);
				$surat_masuk->setWaktu_terima($row['waktu_terima_sm']);
				$surat_masuk->setPenerima($row['penerima_sm']);
				$surat_masuk->setNo_agenda($row['no_agenda_sm']);
				
			}
		}	
		$list_surat_masuk[]=$surat_masuk;
		}	
		return $list_surat_masuk; 		
		
	}
	
	
	
	function getById($id){
		$sql="
		select *
		from
		surat_masuk
		where 
		kd_sm = '$id'
		";
		$surat_masuk=false;
		$data = mysql_query($sql);
		if($data){
			while($row = mysql_fetch_assoc($data)){
			
				$surat_masuk = new Surat_masuk();
				$surat_masuk->setId($row['kd_sm']);
				$surat_masuk->setNomor($row['no_sm']);
				$surat_masuk->setTgl($row['tgl_sm']);
				$surat_masuk->setSifat($row['kd_sifat_surat']);
				$surat_masuk->setHal($row['hal_sm']);
				$surat_masuk->setLampiran($row['lampiran_sm']);
				$surat_masuk->setAsaL($row['asal_sm']);
				$surat_masuk->setWaktu_terima($row['waktu_terima_sm']);
				$surat_masuk->setPenerima($row['penerima_sm']);
				$surat_masuk->setNo_agenda($row['no_agenda_sm']);
				
				
			}
		}	
		return $surat_masuk;
	}
	
	
	function add(Surat_masuk $surat_masuk){
	
		$sql="
		select max(no_agenda_sm) as no_agenda from surat_masuk	where YEAR(waktu_terima_sm) = YEAR(now())	
		";
		$query=mysql_query($sql);
        $data=mysql_fetch_assoc($query);
		$agenda = "";
		if ($data['no_agenda'] == null){
          $agenda = 1;
        } else {
			$agenda = $data['no_agenda']+1;
		}
			
		$sql="insert 
		into 
		surat_masuk(kd_sm, no_sm, tgl_sm, kd_sifat_surat, hal_sm, lampiran_sm, asal_sm, waktu_terima_sm, penerima_sm,
		no_agenda_sm)
		values(
		'',
		'".$surat_masuk->getNomor()."',
		'".$surat_masuk->getTgl()."',
		'".$surat_masuk->getSifat()."',
		'".$surat_masuk->getHal()."',
		'".$surat_masuk->getLampiran()."',
		'".$surat_masuk->getAsaL()."',
		'".$surat_masuk->getWaktu_terima()."',
		'".$surat_masuk->getPenerima()."',
		'".$agenda."'
		)
		";
		$query=mysql_query($sql);
	
	}
	
	function edit(Surat_masuk $surat_masuk){
		//var_dump($surat_masuk);
		$sql="update surat_masuk set 
		no_sm='".$surat_masuk->getNomor()."',
		tgl_sm='".$surat_masuk->getTgl()."', 
		kd_sifat_surat='".$surat_masuk->getSifat()."', 
		hal_sm='".$surat_masuk->getHal()."', 
		lampiran_sm='".$surat_masuk->getLampiran()."', 
		asal_sm='".$surat_masuk->getAsaL()."'
		where kd_sm='".$surat_masuk->getId()."'";
		$query=mysql_query($sql);
	}
	
	function delete($id){
		$sql="
		delete from surat_masuk where kd_sm = '$id'		
		";
		$query = mysql_query($sql);
		return $query;
	}


}